package com.app.milkteaapp.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import com.app.milkteaapp.bean.AddCommentGsonBean;
import com.app.milkteaapp.R;
import com.app.milkteaapp.utils.GsonUtils;
import com.app.milkteaapp.utils.MyTools;
import com.app.milkteaapp.utils.OkHttpUtils;
import com.app.milkteaapp.utils.SpUtils;

import org.jetbrains.annotations.NotNull;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.Response;

public class AddCommentActivity extends AppCompatActivity implements View.OnClickListener {

    private EditText et_comment;
    private EditText et_score;
    private Button btn_confirm;
    private int score;
    private String comment;
    Context context = this;
    Intent intent;
    private int milkTeaId;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_add_comment);
        initView();
        this.intent = getIntent();
        Intent intent = this.intent;
        milkTeaId = intent.getIntExtra("teaid", -1);
    }

    private void initView() {
        et_comment = (EditText) findViewById(R.id.et_comment);
        et_score = (EditText) findViewById(R.id.et_score);
        btn_confirm = (Button) findViewById(R.id.btn_confirm);

        btn_confirm.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_confirm:
                if(submit()){
                    //拿系统时间   拿用户名
                    SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                    String dateString = simpleDateFormat.format(new Date());

                    int userid = SpUtils.getUserBean(this).getId();

                    JSONObject jsonObject = new JSONObject();
                    try {
                        jsonObject.put("comment",comment);
                        jsonObject.put("date",dateString);
                        jsonObject.put("score",score);
                        jsonObject.put("userid",userid);
                        jsonObject.put("teaid",milkTeaId);

                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                    Log.d("_______________", "onClick: "+jsonObject.toString());
                    OkHttpUtils.MyCall(MyTools.URL+"/comment/add",jsonObject.toString()).enqueue(new Callback() {
                        @Override
                        public void onFailure(@NotNull Call call, @NotNull IOException e) {
                            runOnUiThread(new Runnable() {
                                @Override
                                public void run() {
                                    Toast.makeText(context, "网络炸了", Toast.LENGTH_SHORT).show();
                                }
                            });
                        }

                        @Override
                        public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException {
                            //gson解析
                            String string = response.body().string();
                            AddCommentGsonBean addCommentGsonBean = GsonUtils.fromJson(string,AddCommentGsonBean.class);

                            if(addCommentGsonBean.isSuccess()){
                                finish();
                            }

                        }
                    });
                }

                break;
        }
    }

    private boolean submit() {
        // validate
        comment = et_comment.getText().toString().trim();
        String s = et_score.getText().toString().trim();

        if (TextUtils.isEmpty(comment)) {
            Toast.makeText(this, "comment不能为空", Toast.LENGTH_SHORT).show();
        }else if (TextUtils.isEmpty(s)) {
            Toast.makeText(this, "score不能为空", Toast.LENGTH_SHORT).show();
        }else{
            score = Integer.parseInt(s);
            if (score<0||score>10) {
                Toast.makeText(this, "评分范围为0-10", Toast.LENGTH_SHORT).show();
            }else {
                return true;
            }
        }
        return false;

    }
}
